<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件分享 - Snapan</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }

        .share-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .share-header {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            text-align: center;
        }

        .share-title {
            font-size: 2.5rem;
            font-weight: 700;
            color: #2d3748;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .share-subtitle {
            font-size: 1.1rem;
            color: #718096;
            margin-bottom: 20px;
        }

        .share-info {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            flex-wrap: wrap;
        }

        .info-item {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 16px;
            background: rgba(102, 126, 234, 0.1);
            border-radius: 20px;
            font-size: 0.9rem;
            color: #4a5568;
        }

        .file-list-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
        }

        .file-list-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 2px solid #e2e8f0;
        }

        .file-list-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #2d3748;
        }

        .file-count {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.9rem;
            font-weight: 500;
        }

        .file-item {
            display: flex;
            align-items: center;
            padding: 20px;
            margin-bottom: 15px;
            background: #f8fafc;
            border-radius: 12px;
            border: 1px solid #e2e8f0;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .file-item:hover {
            background: #edf2f7;
            border-color: #cbd5e0;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .file-icon {
            font-size: 2.5rem;
            margin-right: 20px;
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea, #764ba2);
            border-radius: 12px;
            color: white;
        }

        .file-info {
            flex: 1;
            min-width: 0;
        }

        .file-name {
            font-size: 1.1rem;
            font-weight: 600;
            color: #2d3748;
            margin-bottom: 5px;
            word-break: break-all;
        }

        .file-meta {
            display: flex;
            gap: 20px;
            font-size: 0.9rem;
            color: #718096;
        }

        .file-actions {
            display: flex;
            gap: 10px;
        }

        .action-btn {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .download-btn {
            background: linear-gradient(135deg, #48bb78, #38a169);
            color: white;
        }

        .download-btn:hover {
            background: linear-gradient(135deg, #38a169, #2f855a);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(72, 187, 120, 0.3);
        }

        .preview-btn {
            background: linear-gradient(135deg, #4299e1, #3182ce);
            color: white;
        }

        .preview-btn:hover {
            background: linear-gradient(135deg, #3182ce, #2c5282);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(66, 153, 225, 0.3);
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #718096;
        }

        .empty-icon {
            font-size: 4rem;
            margin-bottom: 20px;
        }

        .loading {
            text-align: center;
            padding: 60px 20px;
            color: #718096;
        }

        .loading-spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #e2e8f0;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error-state {
            text-align: center;
            padding: 60px 20px;
            color: #e53e3e;
        }

        .error-icon {
            font-size: 4rem;
            margin-bottom: 20px;
        }

        .retry-btn {
            background: linear-gradient(135deg, #e53e3e, #c53030);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            font-size: 1rem;
            cursor: pointer;
            margin-top: 20px;
        }

        .retry-btn:hover {
            background: linear-gradient(135deg, #c53030, #9c2626);
        }

        @media (max-width: 768px) {
            .share-container {
                padding: 15px;
            }

            .share-header {
                padding: 20px;
            }

            .share-title {
                font-size: 2rem;
            }

            .share-info {
                flex-direction: column;
                gap: 10px;
            }

            .file-item {
                flex-direction: column;
                text-align: center;
                gap: 15px;
            }

            .file-icon {
                margin-right: 0;
            }

            .file-meta {
                justify-content: center;
            }

            .file-actions {
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="share-container">
        <!-- 分享页面头部 -->
        <div class="share-header">
            <h1 class="share-title">📁 文件分享</h1>
            <div class="share-info">
                <div class="info-item">
                    <span>🔒</span>
                    <span>安全分享</span>
                </div>
                <div class="info-item">
                    <span>⚡</span>
                    <span>快速下载</span>
                </div>
                <div class="info-item">
                    <span>🌐</span>
                    <span>无需登录</span>
                </div>
            </div>
        </div>

        <!-- 文件列表容器 -->
        <div class="file-list-container">
            <div class="file-list-header">
                <h2 class="file-list-title">分享文件</h2>
                <div class="file-count" id="fileCount">加载中...</div>
            </div>

            <!-- 加载状态 -->
            <div id="loadingState" class="loading">
                <div class="loading-spinner"></div>
                <p>正在加载文件列表...</p>
            </div>

            <!-- 空状态 -->
            <div id="emptyState" class="empty-state" style="display: none;">
                <div class="empty-icon">📂</div>
                <p>暂无分享文件</p>
            </div>

            <!-- 错误状态 -->
            <div id="errorState" class="error-state" style="display: none;">
                <div class="error-icon">❌</div>
                <p id="errorMessage">加载失败</p>
                <button class="retry-btn" onclick="loadShareFiles()">重试</button>
            </div>

            <!-- 文件列表 -->
            <div id="fileList" style="display: none;">
                <!-- 文件项将通过JavaScript动态生成 -->
            </div>
        </div>
    </div>

    <script>
        // 获取分享码
        const urlParams = new URLSearchParams(window.location.search);
        const shareCode = urlParams.get('code') || window.location.pathname.split('/').pop();
        
        // 页面加载完成后获取分享文件
        document.addEventListener('DOMContentLoaded', function() {
            loadShareFiles();
        });

        // 加载分享文件列表
        async function loadShareFiles() {
            try {
                showLoading();
                
                const response = await fetch(`/api/share/access/${shareCode}`);
                const result = await response.json();
                
                if (result.success) {
                    displayFiles(result.data);
                } else {
                    showError(result.message || '获取分享文件失败');
                }
            } catch (error) {
                console.error('加载分享文件失败:', error);
                showError('网络错误，请稍后重试');
            }
        }

        // 显示加载状态
        function showLoading() {
            document.getElementById('loadingState').style.display = 'block';
            document.getElementById('emptyState').style.display = 'none';
            document.getElementById('errorState').style.display = 'none';
            document.getElementById('fileList').style.display = 'none';
        }

        // 显示错误状态
        function showError(message) {
            document.getElementById('loadingState').style.display = 'none';
            document.getElementById('emptyState').style.display = 'none';
            document.getElementById('errorState').style.display = 'block';
            document.getElementById('fileList').style.display = 'none';
            document.getElementById('errorMessage').textContent = message;
        }

        // 显示空状态
        function showEmpty() {
            document.getElementById('loadingState').style.display = 'none';
            document.getElementById('emptyState').style.display = 'block';
            document.getElementById('errorState').style.display = 'none';
            document.getElementById('fileList').style.display = 'none';
        }

        // 显示文件列表
        function displayFiles(shareData) {
            document.getElementById('loadingState').style.display = 'none';
            document.getElementById('emptyState').style.display = 'none';
            document.getElementById('errorState').style.display = 'none';
            document.getElementById('fileList').style.display = 'block';

            const fileList = shareData.files || [];
            const fileListContainer = document.getElementById('fileList');
            const fileCount = document.getElementById('fileCount');

            // 更新文件数量
            fileCount.textContent = `${fileList.length} 个文件`;

            if (fileList.length === 0) {
                showEmpty();
                return;
            }

            // 生成文件列表HTML
            fileListContainer.innerHTML = fileList.map(file => `
                <div class="file-item">
                    <div class="file-icon">${getFileIcon(file.fileName, file.isDirectory)}</div>
                    <div class="file-info">
                        <div class="file-name">${file.fileName}</div>
                        <div class="file-meta">
                            <span>📅 ${formatDate(file.createTime)}</span>
                            <span>📏 ${formatFileSize(file.fileSize)}</span>
                            <span>👤 ${file.userName || '未知用户'}</span>
                        </div>
                    </div>
                    <div class="file-actions">
                        ${file.isDirectory ? '' : `
                            <button class="action-btn download-btn" onclick="downloadFile('${file.id}')">
                                📥 下载
                            </button>
                        `}
                    </div>
                </div>
            `).join('');
        }

        // 获取文件图标
        function getFileIcon(fileName, isDirectory) {
            if (isDirectory) return '📁';
            
            const ext = fileName.split('.').pop().toLowerCase();
            const iconMap = {
                'pdf': '📄',
                'doc': '📝', 'docx': '📝',
                'xls': '📊', 'xlsx': '📊',
                'ppt': '📋', 'pptx': '📋',
                'txt': '📄', 'md': '📄',
                'jpg': '🖼️', 'jpeg': '🖼️', 'png': '🖼️', 'gif': '🖼️',
                'mp4': '🎬', 'avi': '🎬', 'mov': '🎬',
                'mp3': '🎵', 'wav': '🎵',
                'zip': '📦', 'rar': '📦', '7z': '📦'
            };
            
            return iconMap[ext] || '📄';
        }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (!bytes) return '0 B';
            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
        }

        // 格式化日期
        function formatDate(dateString) {
            const date = new Date(dateString);
            return date.toLocaleDateString('zh-CN', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit'
            });
        }

        // 下载文件
        async function downloadFile(fileId) {
            try {
                // 直接重定向到分享下载接口，它会验证权限后重定向到FileBaseController
                window.location.href = `/api/share/download/${shareCode}/${fileId}`;
            } catch (error) {
                console.error('下载失败:', error);
                alert('下载失败，请稍后重试');
            }
        }
    </script>
</body>
</html>
